﻿/*
==================================================================
    注意：此文件为Auto代码，不可手动修改【用户】
==================================================================
*/

namespace Services
{
    /// <summary>
    /// Auto用户
    /// </summary>
    public abstract class AutoUserMapping : BaseMapping<User, UserModel, UserFilterModel>
    {
        public AutoUserMapping(ICurrentUser currentUser)
            : base(currentUser) { }

        #region 读数据
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entity">Entity对象</param>
        /// <returns></returns>
        public override UserModel Entity2Model(User entity)
        {
            UserModel model = new UserModel();

            model.UserID = entity.UserID;
            model.GroupInfoID = entity.GroupInfoID;
            model.LoginName = entity.LoginName;
            model.LoginPassword = entity.LoginPassword;
            model.Name = entity.Name;
            model.JobCode = entity.JobCode;
            model.Email = entity.Email;
            model.Phone = entity.Phone;
            model.IdCard = entity.IdCard;
            model.InDate = entity.InDate;
            model.IsDelete = entity.IsDelete;
            model.IsInit = entity.IsInit;
            model.CreateUserID = entity.CreateUserID;
            model.CreateUserName = entity.CreateUserName;
            model.CreateDate = entity.CreateDate;
            model.UpdateUserID = entity.UpdateUserID;
            model.UpdateUserName = entity.UpdateUserName;
            model.UpdateDate = entity.UpdateDate;

            return model;
        }
        #endregion

        #region 写数据
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="model">ViewModel对象</param>
        /// <returns></returns>
        public override User Model2Entity(UserModel model)
        {
            User entity = new User();

            entity.UserID = model.UserID;
            entity.GroupInfoID = model.GroupInfoID;
            entity.LoginName = model.LoginName;
            entity.LoginPassword = model.LoginPassword;
            entity.Name = model.Name;
            entity.JobCode = model.JobCode;
            entity.Email = model.Email;
            entity.Phone = model.Phone;
            entity.IdCard = model.IdCard;
            entity.InDate = model.InDate;
            entity.IsDelete = model.IsDelete;
            entity.IsInit = model.IsInit;
            entity.CreateUserID = model.CreateUserID;
            entity.CreateUserName = model.CreateUserName;
            entity.CreateDate = model.CreateDate;
            entity.UpdateUserID = model.UpdateUserID;
            entity.UpdateUserName = model.UpdateUserName;
            entity.UpdateDate = model.UpdateDate;

            return entity;
        }
        #endregion

        #region 搜索条件转换
        /// <summary>
        /// 搜索条件转换，从FilterModel对象转换成Where对象
        /// </summary>
        /// <param name="filterModel">FilterModel对象</param>
        /// <returns></returns>
        public override Expressionable<User> FilterModel2Where(UserFilterModel filterModel)
        {
            var where = base.FilterModel2Where(filterModel);

            //编号
            if (filterModel._UserID.HasValue)
            {
                where = where.And(x => x.UserID == filterModel._UserID.Value);
            }
            if (filterModel._UserIDs.IsNotEmpty() && filterModel._UserIDs.Count > 0)
            {
                where = where.And(x => filterModel._UserIDs.Contains(x.UserID));
            }
            if (filterModel._NotUserIDs.IsNotEmpty() && filterModel._NotUserIDs.Count > 0)
            {
                where = where.And(x => !filterModel._NotUserIDs.Contains(x.UserID));
            }

            //集团
            if (filterModel._GroupInfoID.HasValue)
            {
                where = where.And(x => x.GroupInfoID == filterModel._GroupInfoID.Value);
            }
            if (filterModel._GroupInfoIDs.IsNotEmpty() && filterModel._GroupInfoIDs.Count > 0)
            {
                where = where.And(x => filterModel._GroupInfoIDs.Contains(x.GroupInfoID));
            }
            if (filterModel._NotGroupInfoIDs.IsNotEmpty() && filterModel._NotGroupInfoIDs.Count > 0)
            {
                where = where.And(x => !filterModel._NotGroupInfoIDs.Contains(x.GroupInfoID));
            }

            //登录名
            if (!string.IsNullOrEmpty(filterModel._LoginName))
            {
                where = where.And(x => x.LoginName.Contains(filterModel._LoginName));
            }
            if (!string.IsNullOrEmpty(filterModel._LoginNameEqual))
            {
                where = where.And(x => x.LoginName == filterModel._LoginNameEqual);
            }

            //姓名
            if (!string.IsNullOrEmpty(filterModel._Name))
            {
                where = where.And(x => x.Name.Contains(filterModel._Name));
            }
            if (!string.IsNullOrEmpty(filterModel._NameEqual))
            {
                where = where.And(x => x.Name == filterModel._NameEqual);
            }

            //工号
            if (!string.IsNullOrEmpty(filterModel._JobCode))
            {
                where = where.And(x => x.JobCode.Contains(filterModel._JobCode));
            }
            if (!string.IsNullOrEmpty(filterModel._JobCodeEqual))
            {
                where = where.And(x => x.JobCode == filterModel._JobCodeEqual);
            }

            //入职日期
            if (filterModel._InDate.HasValue)
            {
                where = where.And(x => x.InDate == filterModel._InDate.Value);
            }
            if (filterModel._InDate_Begin.HasValue)
            {
                where = where.And(x => x.InDate >= filterModel._InDate_Begin.Value);
            }
            if (filterModel._InDate_End.HasValue)
            {
                where = where.And(x => x.InDate < filterModel._InDate_End.Value.AddDays(1));
            }

            //是否删除
            if (filterModel._IsDelete.HasValue)
            {
                where = where.And(x => x.IsDelete == filterModel._IsDelete.Value);
            }
            if (filterModel._IsDeletes.IsNotEmpty() && filterModel._IsDeletes.Count > 0)
            {
                where = where.And(x => filterModel._IsDeletes.Contains(x.IsDelete));
            }
            
            //是否为初始化数据
            if (filterModel._IsInit.HasValue)
            {
                where = where.And(x => x.IsInit == filterModel._IsInit.Value);
            }
            if (filterModel._IsInits.IsNotEmpty() && filterModel._IsInits.Count > 0)
            {
                where = where.And(x => filterModel._IsInits.Contains(x.IsInit));
            }
            
            //创建人姓名
            if (!string.IsNullOrEmpty(filterModel._CreateUserName))
            {
                where = where.And(x => x.CreateUserName.Contains(filterModel._CreateUserName));
            }
            if (!string.IsNullOrEmpty(filterModel._CreateUserNameEqual))
            {
                where = where.And(x => x.CreateUserName == filterModel._CreateUserNameEqual);
            }

            //创建时间
            if (filterModel._CreateDate.HasValue)
            {
                where = where.And(x => x.CreateDate == filterModel._CreateDate.Value);
            }
            if (filterModel._CreateDate_Begin.HasValue)
            {
                where = where.And(x => x.CreateDate >= filterModel._CreateDate_Begin.Value);
            }
            if (filterModel._CreateDate_End.HasValue)
            {
                where = where.And(x => x.CreateDate < filterModel._CreateDate_End.Value.AddDays(1));
            }

            //修改人姓名
            if (!string.IsNullOrEmpty(filterModel._UpdateUserName))
            {
                where = where.And(x => x.UpdateUserName.Contains(filterModel._UpdateUserName));
            }
            if (!string.IsNullOrEmpty(filterModel._UpdateUserNameEqual))
            {
                where = where.And(x => x.UpdateUserName == filterModel._UpdateUserNameEqual);
            }

            //修改时间
            if (filterModel._UpdateDate.HasValue)
            {
                where = where.And(x => x.UpdateDate == filterModel._UpdateDate.Value);
            }
            if (filterModel._UpdateDate_Begin.HasValue)
            {
                where = where.And(x => x.UpdateDate >= filterModel._UpdateDate_Begin.Value);
            }
            if (filterModel._UpdateDate_End.HasValue)
            {
                where = where.And(x => x.UpdateDate < filterModel._UpdateDate_End.Value.AddDays(1));
            }

            return where;
        }
        #endregion
    }
}

